<template>
  <div class="tab-nav-container">
    <div class="tab-list">
      <div 
        v-for="(item, index) in breadcrumbs" 
        :key="index"
        :class="['tab-item', { active: isActive(item.name) }]"
        @click="handleClick(index)"
      >
        {{ item.title }}
        <i 
          v-if="breadcrumbs.length > 1 && item.name !== 'designIndex'" 
          class="el-icon-close close-icon"
          @click.stop="closeCurrent(index)"
        ></i>
      </div>
    </div>
  </div>
</template>

<script>
import { get } from 'lodash'
import { sessionStorage } from '@/deps/utils'

export default {
  name: 'BaseBreadcrumb',
  data() {
    return {
      breadcrumbs: []
    }
  },
  created() {
    const savedBreadcrumbs = sessionStorage('breadcrumbs')
    if (savedBreadcrumbs) {
      this.breadcrumbs = savedBreadcrumbs
    }
    this.$bus.$on('switch-component', this.changeCom)
  },
  methods: {
    handleClick(index) {
      const item = this.breadcrumbs[index]
      if (!this.isActive(item.name)) {
        this.$emit('switch-component', item.name) // 触发组件切换
      }
    },
    closeCurrent(index) {
      if (this.breadcrumbs.length <= 1) return
      const isClosingCurrent = this.breadcrumbs[index].name === this.$parent.activeComponent
      this.breadcrumbs.splice(index, 1)
      this.saveBreadcrumbs()
      if (isClosingCurrent) {
        const nextItem = this.breadcrumbs[index] || this.breadcrumbs[index - 1]
        if (nextItem) {
          this.$emit('switch-component', nextItem.name)
        }
      }
    },
    isActive(name) {
      return get(this.$parent, 'activeComponent') === name
    },
    changeCom(data) {
      if (data.name === this.activeComponent) return
      const breadcrumb = this.breadcrumbs.find(item => item.name === data.name)
      if (!breadcrumb) {
        this.breadcrumbs.push(data)
        this.saveBreadcrumbs()
      } else {
        breadcrumb.title = data.title
      }
    },
    saveBreadcrumbs() {
      sessionStorage('breadcrumbs', this.breadcrumbs)
    },
    clearBreadcrumbs() {
      this.breadcrumbs = []
      sessionStorage('breadcrumbs', null)
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-nav-container {
  position: fixed;
  bottom: 20px;
  left: 50%;
  z-index: 1000;
  background-color: var(--bg-color);
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3);
  transform: translate(-50%, 0%);
  
  .tab-list {
    display: flex;
    align-items: stretch;
    height: 40px;
    padding: 2px;
  }

  .tab-item {
    position: relative;
    display: inline-flex;
    align-items: center;
    padding: 0 20px;
    height: 100%;
    line-height: 36px;
    font-size: 14px;
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.3s;
    white-space: nowrap;
    border-right: 1px solid rgba(255, 255, 255, 0.1);

    &:last-child {
      border-right: none;
    }

    &:hover {
      background-color: var(--bg-hover-color);
    }

    &.active {
      background-color: var(--color-primary);
      color: #fff;

      .close-icon {
        color: #fff;
      }
    }

    .close-icon {
      margin-left: 8px;
      font-size: 12px;
      
      &:hover {
        color: #f56c6c;
      }
    }
  }
}

// 主题样式适配
:global(.theme-black), :global(.theme-white) {
  .tab-nav-container {
    background-color: var(--bg-color);

    .tab-item {
      background-color: var(--bg-color);
      color: var(--text-color);

      &:hover {
        background-color: var(--bg-hover-color);
      }

      &.active {
        background-color: var(--color-primary);
        color: #fff;
      }
    }
  }
}
</style>